function OffsetsCell = NevadaAllOffsets(url)
% NevadaAllSteps - Takes all steps from Nevada database
% 
%   OffsetsCell = NevadaAllOffsets
%   OffsetsCell = NevadaAllOffsets(URL)
%   OffsetsCell = NevadaAllOffsets(FILENA)
%
% Takes all the steps from the Nevada Geodetic database or from a database
% managed in a similar way.
%
% Options about the input argument:
% - if the input argument is undefined or empty, the default URL, i.e. the
%   Nevada's one (http://geodesy.unr.edu/NGLStationPages/steps.txt), is 
%   used to take the data leading to the ASCII file steps.txt;
% - if URL is an url, the data are taken from the online file which
%   corresponds to such a URL, leading to the ASCII file steps.txt;
% - if FILENA is the name of an ASCII file arranged as the Nevada's one, 
%   the data are taken from such a text file.
% 
% The output OffsetsCell is the N-by-3 cell variable whose columns are
%       {StatName, Date, typeEvent},
% where StatName are the station names, Date are the dates, typeEvent is 1
% in the case of change antenna/receiver/other changes, and 2 in the case
% of a relatively near earthquake.
% 
% See also OffsetsfromCell.

% G. Teza, 2022

if nargin < 1 || isempty(url)
    url = 'http://geodesy.unr.edu/NGLStationPages/steps.txt';
end

try 
    filena = 'steps.txt';
    websave(filena,url);
catch
    filena = url;
end

n = linecount(filena);  % ancillary function, see below 

fid = fopen(filena);

c = cell(n,3);
for k = 1:n
    tlinek = fgetl(fid);
    if numel(tlinek) >= 16
        namelk = tlinek(1:4);
        datelk = tlinek(7:13);
        indexlk = tlinek(16);
        c(k,1) = {namelk};
        c(k,2) = {datelk};
        c(k,3) = {indexlk};
    end
end

fclose(fid);

OffsetsCell = c;

%% Ancillary functions

function n = linecount(filena)
% counts the lines of the ASCII file filena
fid = fopen(filena);
n = 0;
tline = fgetl(fid);
while ischar(tline)
    tline = fgetl(fid);
    n = n+1;
end
fclose(fid); 